package com.jdew.thkj.fragment;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jdew.thkj.R;
import com.jdew.thkj.activity.BiTuiActivity;
import com.jdew.thkj.activity.LoginActivity;
import com.jdew.thkj.activity.MainActivity;
import com.jdew.thkj.activity.SortsActivity;
import com.jdew.thkj.activity.TbProductDetailActivity;
import com.jdew.thkj.adapter.BituiAdapter;
import com.jdew.thkj.adapter.MainAdapter;
import com.jdew.thkj.adapter.WrapAdapter;
import com.jdew.thkj.base.BaseFragment;
import com.jdew.thkj.bean.ZhaoshangBean;
import com.jdew.thkj.manager.WrapContentGridLayoutManager;
import com.jdew.thkj.manager.WrapContentLinearLayoutManager;
import com.jdew.thkj.onitemInterface.OnBeforeRefresh;
import com.jdew.thkj.onitemInterface.OnItemClickListener;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by yangzai on 2018/1/23 0023.
 */

public class MainFragment extends BaseFragment implements View.OnClickListener, OnBeforeRefresh {
    @Bind(R.id.swipe_target)
    RecyclerView recycleView;
    @Bind(R.id.search)
    TextView search;
    @Bind(R.id.netclick)
    TextView netclick;
    @Bind(R.id.resultclick)
    TextView resultclick;
    @Bind(R.id.swipelayout)
    LinearLayout swipelayout;
    @Bind(R.id.tv_load_prompt)
    TextView tv_load_prompt;
    @Bind(R.id.swipe_load_more_footer)
    LinearLayout swipeLoadMoreFooter;
    @Bind(R.id.noresult)
    LinearLayout noresult;
    @Bind(R.id.arror_top)
    ImageView arrorTop;
    @Bind(R.id.tvRefresh)
    TextView tvRefresh;
    @Bind(R.id.search_view)
    RelativeLayout searchView;
    private ImageView nvzhuang, muying, jiaju, nanzhuang, xiebao, shuma, meishi, hufu, meizhuang, qita;
    private List<ZhaoshangBean.DataBean.TbkUatmFavoritesItemGetResponseBean.ResultsBean.UatmTbkItemBean> list = new ArrayList<>();
    private List<ZhaoshangBean.DataBean.TbkUatmFavoritesItemGetResponseBean.ResultsBean.UatmTbkItemBean> list3 = new ArrayList<>();
    private WrapAdapter<MainAdapter> madapter;
    private MainAdapter adapter;
    private int page = 1;
    private String ItemId;
    private MainActivity activity;
    private BituiAdapter bituiAdapter;
    private Banner banner;
    private TextView more;
    private RecyclerView bitui;
    private List<Object> listmap = new ArrayList<>();

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_main;
    }

    @Override
    protected void onCreateViewT() {
        activity = (MainActivity) getActivity();
        adapter = new MainAdapter(activity, list);
        madapter = new WrapAdapter<>(adapter);
        bituiAdapter = new BituiAdapter(activity, list3);
        addHeaderView();
        recycleView.setLayoutManager(new WrapContentGridLayoutManager(activity, 2, GridLayoutManager.VERTICAL, false));
        bitui.setLayoutManager(new WrapContentLinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false));
        madapter.adjustSpanSize(recycleView);
        resultclick.setOnClickListener(this);
        netclick.setOnClickListener(this);
        search.setOnClickListener(this);
        arrorTop.setOnClickListener(this);
        recycleView.setAdapter(madapter);
        bitui.setAdapter(bituiAdapter);
        recycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Window window = activity.getWindow();
                int firstVisibleItem = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                }
                if (firstVisibleItem == 0) {
                    search.setBackground(getResources().getDrawable(R.mipmap.main_search_w));
                    searchView.setBackgroundColor(getResources().getColor(R.color.transparent));
                } else {
                    search.setBackground(getResources().getDrawable(R.mipmap.main_search_b));
                    searchView.setBackgroundColor(getResources().getColor(R.color.color_ffffff));
                }
            }
        });
        bituiAdapter.setItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (!RegexValidateUtils.isFastClick()) {
                    return;
                }
                if (activity.sp.getString("SpareTwo", "").equals("")) {
                    Intent in = new Intent(activity, LoginActivity.class);
                    activity.startActivity(in);
                } else if (activity.sp.getString("UserSjPhone", "").equals("") && (!activity.sp.getString("SpareTwo", "").equals("1"))) {
                    activity.toset("检测到您还没有绑定上级,请到我的页面绑定上级");
                } else {
                    ItemId = list3.get(position).getNum_iid();
                    toDetail();
                }
            }
        });
        adapter.setItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (!RegexValidateUtils.isFastClick()) {
                    return;
                }
                if (activity.sp.getString("SpareTwo", "").equals("")) {
                    startActivity(new Intent(activity, LoginActivity.class));
                } else if (activity.sp.getString("UserSjPhone", "").equals("") && (!activity.sp.getString("SpareTwo", "").equals("1"))) {
                    activity.toset("检测到您还没有绑定上级,请到我的页面绑定上级");
                } else {
                    ItemId = list.get(position).getNum_iid();
                    toDetail();
                }
            }
        });
    }


    private void addHeaderView() {
        LayoutInflater inflater = LayoutInflater.from(activity);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.layout_header, null);
        banner = (Banner) layout.findViewById(R.id.banner);
        bitui = (RecyclerView) layout.findViewById(R.id.bitui);
        nvzhuang = (ImageView) layout.findViewById(R.id.nvzhaung);
        muying = (ImageView) layout.findViewById(R.id.muying);
        jiaju = (ImageView) layout.findViewById(R.id.jiaju);
        nanzhuang = (ImageView) layout.findViewById(R.id.nanzhuang);
        xiebao = (ImageView) layout.findViewById(R.id.xiebao);
        shuma = (ImageView) layout.findViewById(R.id.shuma);
        meishi = (ImageView) layout.findViewById(R.id.meishi);
        hufu = (ImageView) layout.findViewById(R.id.hufu);
        meizhuang = (ImageView) layout.findViewById(R.id.meizhuang);
        qita = (ImageView) layout.findViewById(R.id.qita);
        more = (TextView) layout.findViewById(R.id.more);
        nvzhuang.setOnClickListener(this);
        muying.setOnClickListener(this);
        jiaju.setOnClickListener(this);
        nanzhuang.setOnClickListener(this);
        xiebao.setOnClickListener(this);
        shuma.setOnClickListener(this);
        meishi.setOnClickListener(this);
        hufu.setOnClickListener(this);
        meizhuang.setOnClickListener(this);
        qita.setOnClickListener(this);
        more.setOnClickListener(this);
        madapter.addHeaderView(layout);
//        getBanner();
//        bitui();
    }

//    public void bitui() {
//        RetrofitManager.getInstance(AppConfig.BASE_URL).createReq(RequestApi.class).GetWillpush(1,MD5Utils.getMD5("S"+activity.sp.getString("seceret","")+"PageIndex"+1+activity.sp.getString("seceret","")+"S").toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<ZhaoshangBean>() {
//            @Override
//            public void onCompleted() {
//            }
//
//            @Override
//            public void onError(Throwable e) {
//            }
//
//            @Override
//            public void onNext(ZhaoshangBean bean) {
//                list3.clear();
//                if (bean.isSuccess()) {
//                    list3.addAll(bean.getData().getTbk_uatm_favorites_item_get_response().getResults().getUatm_tbk_item());
//                    bituiAdapter.setDataList(list3);
//                }
//            }
//        });
//    }

//    public void getBanner() {
//        RetrofitManager.getInstance(AppConfig.BASE_URL).createReq(RequestApi.class).GetBanner(MD5Utils.getMD5("l"+activity.sp.getString("seceret","")+"Sign"+activity.sp.getString("seceret","")+"l"+activity.sp.getString("seceret","")+"l").toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<LunBoBean>() {
//            @Override
//            public void onCompleted() {
//
//            }
//
//            @Override
//            public void onError(Throwable e) {
//            }
//
//            @Override
//            public void onNext(LunBoBean bean) {
//                if (bean.getCode().equals("0")) {
//                    listmap.clear();
//                    listmap.addAll(bean.getData());
//                    banner.setImageLoader(new MyLoader());
//                    banner.setIndicatorGravity(BannerConfig.CENTER); //指示点的位置
//                    banner.setImages(listmap);
//                    banner.setDelayTime(5000);
//                    banner.isAutoPlay(true);
//                    banner.start();
//                }
//            }
//        });
//    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void initData() {

    }

    @Override
    public void onBeforeRefresh(boolean isShow) {
        search.setVisibility(isShow ? View.GONE : View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.netclick: {
            }
            break;
            case R.id.resultclick: {
            }
            break;
            case R.id.search: {
                activity.viewpager.setCurrentItem(1);
                activity.rb = (RadioButton) activity.rgp.getChildAt(1);
                activity.rb.setChecked(true);
            }
            break;
            case R.id.nvzhaung: {
                Intent in = new Intent(activity, SortsActivity.class);
                in.putExtra("type", 0);
                startActivity(in);
            }
            break;
            case R.id.muying: {
                Intent in = new Intent(activity, SortsActivity.class);
                in.putExtra("type", 1);
                startActivity(in);
            }
            break;
            case R.id.jiaju: {
                Intent in = new Intent(activity, SortsActivity.class);
                in.putExtra("type", 2);
                startActivity(in);
            }
            break;
            case R.id.nanzhuang: {
                Intent in = new Intent(activity, SortsActivity.class);
                in.putExtra("type", 3);
                startActivity(in);
            }
            break;
            case R.id.xiebao: {
                Intent in = new Intent(activity, SortsActivity.class);
                in.putExtra("type", 4);
                startActivity(in);
            }
            break;
            case R.id.shuma: {
                Intent in = new Intent(activity, SortsActivity.class);
                in.putExtra("type", 5);
                startActivity(in);
            }
            break;
            case R.id.meishi: {
                Intent in = new Intent(activity, SortsActivity.class);
                in.putExtra("type", 6);
                startActivity(in);
            }
            break;
            case R.id.hufu: {
                Intent in = new Intent(activity, SortsActivity.class);
                in.putExtra("type", 7);
                startActivity(in);
            }
            break;
            case R.id.meizhuang: {
                Intent in = new Intent(activity, SortsActivity.class);
                in.putExtra("type", 8);
                startActivity(in);
            }
            break;
            case R.id.qita: {
                Intent in = new Intent(activity, SortsActivity.class);
                in.putExtra("type", 9);
                startActivity(in);
            }
            break;
            case R.id.more: {
                Intent in = new Intent(activity, BiTuiActivity.class);
                startActivity(in);
            }
            break;
            case R.id.arror_top: {
                recycleView.smoothScrollToPosition(0);
                arrorTop.setVisibility(View.GONE);
            }
            break;
            default:
                break;
        }

    }


    //自定义的图片加载器
    private class MyLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            if (null != context) {
                Glide.with(context).load((String) path).into(imageView);
            }
        }
    }

    public void toDetail() {
        Bundle b = new Bundle();
        b.putSerializable("itemId", ItemId);
        startActivity(TbProductDetailActivity.class, b);
    }
}
